package com.google.code.jtasks.server;

import com.google.code.jtasks.server.modules.JacksonConfigurationModule;
import com.google.code.jtasks.server.modules.ServerConfigurationModule;
import com.google.code.jtasks.server.modules.MongoDBConfigurationModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;

/**
 * Guice configuration context listener.
 *
 * <p>This class is used to enable Guice inside the Jetty servlet context.</p>
 */
public class GuiceConfig extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new JerseyServletModule() {
            @Override
            protected void configureServlets() {
                // Install all our custom modules
                install(new JacksonConfigurationModule());
                install(new MongoDBConfigurationModule());
                install(new ServerConfigurationModule());

                // Configure servlets
                bind(GuiceContainer.class);
                serve("/*").with(GuiceContainer.class);
            }
        });
    }
}
